package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.android.widget.lviewpage.SwitchBtnParam;
import com.longrise.bjjt.BaseApplication;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.ApiImpI;

/**
 * Created by Administrator on 2016/8/16.
 */
public class FeedBackView extends LFView implements View.OnClickListener {

    private View view;

    private Context context;

    private EditText contentET;

    private Button confirmBtn;

    private BaseApplication application;

    FeedBackView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter parameter = new FormParameter();
        parameter.setShowtitle(false);
        return parameter;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_feedback, null);
        if (null == view) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("用户反馈");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        contentET = (EditText) view.findViewById(R.id.feedback_et);
        confirmBtn = (Button) view.findViewById(R.id.feedback_btn);

        setListen(true);
        application = (BaseApplication) context.getApplicationContext();
    }

    private void setListen(Boolean flag) {
        if (confirmBtn != null) {
            confirmBtn.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.feedback_btn:
                commit();
                break;
            default:
                break;
        }
    }

    //提交反馈内容
    private void commit() {
        String content = contentET.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            UiUtil.showToast(context, "请输入您的反馈意见或建议！");
            return;
        }
        String userflag = application.getKckpName();
        String token = application.getToken();
        EntityBean baseBean = application.getLoginInfo().getBean("userinfo");
        String phone = baseBean.getString("mobilephone");
        String name = baseBean.getString("name");
        if (TextUtils.isEmpty(name)) {
            name = "未知";
        }
        ApiImpI.getInstance(context).feedback(userflag, token, name, phone, content, new ApiImpI.RequestCallBack() {
            @Override
            public void onError(String error) {
                if (!TextUtils.isEmpty(error)) {
                    UiUtil.showToast(context, error);
                }
            }

            @Override
            public void onSuccess(Object result) {
                EntityBean bean = (EntityBean) result;
                String restate = bean.getString("restate");
                String redes = bean.getString("redes");
                if (TextUtils.equals(restate, "1")) {
                    closeForm();
                    OnDestroy();
                }
                UiUtil.showToast(context, redes);
            }
        });
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListen(false);
        view = null;
    }
}
